home *** CD-ROM | disk | FTP | other *** search
- From: SimsGW@msn.com (Gary Sims)
- Subject: RE: How to initialize an array of structures?
- Date: 12 Jan 96 03:17:18 -0800
- References: <4d240e$nk5@jupiter.planet.net>
- Message-ID: <00001a81+00008c58@msn.com>
- Path: news.msn.com!msn.com
- Newsgroups: comp.lang.c++
- Organization: The Microsoft Network (msn.com)
-
- This is the syntax for assigning a value to one element of an array
- of structures:
-
- static struct FUNCTIONMAP functionlist[300]; // declared with
- appropriate scope
-
-
- functionlist[0]=FUNCTIONMAP("firstfunction",1,3,2,2,2,0,0); // First
- function is index zero
-
- To do that however, you must declare constructors for the structure.
- You need two of them, declared like this in an appropriate header
- file that you can include in any module that uses the FUNCTIONMAP
- structures:
-
- struct FUNCTIONMAP
- {
- // attributes
- char *functionname;
- int functionnumber;
- int maxargs;
- int functarg[5];
- // constructors
- FUNCTIONMAP(); // default constructor
- FUNCTIONMAP(
- const char* Name,
- const int Num, const int Args,
- const int Fa0, const int Fa1,
- const int Fa2, const int Fa3, const int Fa4
- ); // constructor with arguments
- };
-
- and coded something like this in a separate implementation file:
-
- #include "FUNCTIONMAP.h"
- //...
- FUNCTIONMAP::FUNCTIONMAP()
- {
- functionname=NULL; functionnumber=0; maxargs=0;
- for(int i=0; i<5; i++) functarg[i]=0;
- };
-
- FUNCTIONMAP::FUNCTIONMAP(const char* Name,const int Num, const int Args,
- const int Fa0, const int Fa1, const int Fa2, const int Fa3, const int Fa4)
- {
- functionname=NULL;
- if(NULL!=Name) functionname=new char[strlen(Name)+1];
- if(NULL!=functionname) strcpy(functionname,Name);
- functionnumber=Num;
- maxargs=Args;
- functarg[0]=Fa0;
- functarg[1]=Fa1;
- functarg[2]=Fa2;
- functarg[3]=Fa3;
- functarg[4]=Fa4;
- };
-
- I'm sure that the C programming language had some syntax for
- assigning values to the members of a structure, but I don't know what
- it might have been. Since you posted your question in comp.lang.c++ I
- assume you want a C++ answer.
-
- Incidentally, it's uncommon to use the struct type in C++
- programming. It's outdated usage now. A class is the preferred usage.
- You can just substitute "class" for "struct" to re-compile this as a
- class, but then you must add "public:" right after the opening
- bracket to make a class that behaves like a struct (by exposing its
- members to the world at large). Or you can follow C++ programming
- style and provide public methods that do the things you want this
- class to do while keeping its attributes and implementation private.
-
- Changed to a class with a couple of access methods, the declaration
- would like something like this:
-
- class FUNCTIONMAP
- {
- // attributes
- char *functionname;
- int functionnumber;
- int maxargs;
- int functarg[5];
- public:
- // constructors
- FUNCTIONMAP(); // default constructor
- FUNCTIONMAP(
- const char* Name,
- const int Num, const int Args,
- const int Fa0, const int Fa1,
- const int Fa2, const int Fa3, const int Fa4
- ); // constructor with arguments
- // access to data and operations
- const char* FtnName(){return functionname;};
- ResultCode RunThyself();
- };
-
- Good luck,
-
- Gary W. Sims
- Stonehaven Laboratory
-